import Cookies from 'js-cookie'

export function getCookies(name) {
	return Cookies.get(name)
}
export function setCookies(obj) {
	for (const key in obj) {
		Cookies.set(key, obj[key])
	}
}
export function getToken() {
  return Cookies.get(`token`)
}

export function clearCookies() {
	const cookies = document.cookie
	const list = cookies.split('; ') // 解析出名/值对列表
	for (let i = 0; i < list.length; i++) {
		const cookieName = list[i].split('=')[0]
		if (cookieName !== 'deviceCode') Cookies.remove(cookieName)
	}
}
export function setLocalStorage(key, val) {
	return localStorage.setItem(key, val)
}
export function removeLocalStorage(key) {
	return localStorage.removeItem(key)
}
export function clearLocalStorage() {
	return localStorage.clear()
}
export function getLocalStorage(key) {
	return localStorage.getItem(key)
}

export function setSessionStorage(key, val) {
	return sessionStorage.setItem(key, val)
}
export function setAllSessionStorage(obj) {
	for (const key in obj) {
		if (typeof obj[key] === 'object') {
			console.log(obj[key])
			obj[key] = JSON.stringify(obj[key])
		}
		sessionStorage.setItem(key, obj[key])
	}
}
export function removeSessionStorage(key) {
	return sessionStorage.removeItem(key)
}
export function clearSessionStorage() {
	return sessionStorage.clear()
}
export function getSessionStorage(key) {
	return sessionStorage.getItem(key)
}
/* 获取sessionStorage,并以对象的形式返回 */
export function getSessionStorageAll() {
	const session = {}
	Object.keys(sessionStorage).forEach(key => {
		Object.assign(session, {
			[key]: JSON.parse(sessionStorage[key])
		})
    })
	return session
}
/* 获取所有cookies,并以对象的形式返回 */
export function getCookiesAll() {
	const cookies = document.cookie
	const obj = {}
	// 解析出名/值对列表
	cookies.split('; ').forEach(key => {
		const [k, val] = key.split('=')
		Object.assign(obj, {
			[k]: val
		})
    })
	return obj
}
/* 获取localStorage,并以对象的形式返回 */
export function getLocalStorageAll() {
	const local = {}
	Object.keys(localStorage).forEach(key => {
		Object.assign(local, {
			[key]: localStorage[key]
		})
	})
	return local
}